package me.trofoto.romeo.dao;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import me.trofoto.romeo.bean.DrinkRecord;

@Repository
public class DrinkRecordDao extends BaseDao {

	public static final String TABLE_NAME = "drink_record";

	private static final RowMapper<DrinkRecord> ROW_MAPPER = (rs, rowNumber) -> {
		return DrinkRecord.builder().id(rs.getInt("id")).drinkDate(rs.getInt("dr_date"))
				.drinkQuantity(rs.getInt("dr_quantity")).userId(rs.getInt("user_id"))
				.drinkTime(new DateTime(rs.getTimestamp("create_time").getTime())).build();
	};

	public int getDrinkQuantityByDate(int userId, LocalDate date) {
		return getNamedParameterJdbcTemplate().queryForObject(
				"select sum(dr_quantity) sdq from " + TABLE_NAME + " where user_id=:userId and dr_date=:date",
				new MapSqlParameterSource("userId", userId).addValue("date", date.toString("yyyyMMdd")),
				(rs, rowNumber) -> {
					return rs.getInt("sdq");
				});
	}
}
